public class Leet794 {
    public boolean validTicTacToe(String[] board) {
        char[][] chs = new char[3][3];
        chs[0] = board[0].toCharArray();
        chs[1] = board[1].toCharArray();
        chs[2] = board[2].toCharArray();
        int x = 0,o = 0;
        for (char[] ch : chs) {
            for (char c : ch) {
                if (c=='X')
                    x++;
                if (c=='O')
                    o++;
            }
        }
        if (o>x)
            return false;
        if (x>o+1)
            return false;
        for (int i = 0; i < 3; i++) {
            if (chs[i][0]!=' ')
            if (chs[i][0]==chs[i][1]&&chs[i][1]==chs[i][2])
                return false;
            if (chs[0][i]!=' ')
                if (chs[0][i]==chs[1][i]&&chs[1][i]==chs[2][i])
                    return false;
        }
        if (chs[1][1]!=' ')
            if (chs[0][0]==chs[1][1]&&chs[1][1]==chs[2][2])
                return false;
        if (chs[1][1]!=' ')
            return chs[0][2] != chs[1][1] || chs[1][1] != chs[2][0];
        return true;
    }
}
